//============================================================
// Producnt name:		BoBoARTS.CodeMad
// Version: 			1.0
// Coded by:			Shen Bo (bo.shen@jb-aptech.com.cn)
// Auto generated at: 	2010-6-17 17:39:25
//============================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using WCSY.MODEL;

namespace WCSY.DAL
{
	public static partial class Mg_rrRelationService
	{
        public static int AddMg_rrRelation(Mg_rrRelation mg_rrRelation)
		{
            string sql =
				"INSERT mg_rrRelation (roleId, rightsId)" +
				"VALUES (@roleId, @rightsId)";
				
			//sql += " ; SELECT @@IDENTITY";

            try
            {
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@roleId", mg_rrRelation.RoleId),
					new SqlParameter("@rightsId", mg_rrRelation.RightsId)
				};
				
                int issuc = DBHelper.GetScalar(sql, para);
				return issuc;
            }
            catch (Exception e)
            {
				Console.WriteLine(e.Message);
                throw e;
            }
		}
		
        public static void DeleteMg_rrRelation(Mg_rrRelation mg_rrRelation)
		{
            DeleteMg_rrRelationById(mg_rrRelation.RoleId);
		}

        public static void DeleteMg_rrRelationById(string roleId)
		{
            string sql = "DELETE mg_rrRelation WHERE roleId = @roleId";

            try
            {
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@roleId", roleId)
				};
			
                DBHelper.ExecuteCommand(sql, para);
            }
            catch (Exception e)
            {
				Console.WriteLine(e.Message);
				throw e;
            }
		}
		
        public static void ModifyMg_rrRelation(Mg_rrRelation mg_rrRelation)
        {
            string sql =
                "UPDATE mg_rrRelation " +
                "SET " +
	                "roleId = @roleId, " +
	                "rightsId = @rightsId " +
                "WHERE id = @id";


            try
            {
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id", mg_rrRelation.Id),
					new SqlParameter("@roleId", mg_rrRelation.RoleId),
					new SqlParameter("@rightsId", mg_rrRelation.RightsId)
				};

				DBHelper.ExecuteCommand(sql, para);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
				throw e;
            }

        }		

        public static IList<Mg_rrRelation> GetAllMg_rrRelations()
        {
            string sqlAll = "SELECT * FROM mg_rrRelation";
			return GetMg_rrRelationsBySql( sqlAll );
        }

        public static IList<Mg_rrRelation> GetMg_rrRelationById(string roleId)
        {
            string sql = "SELECT * FROM mg_rrRelation WHERE roleId = @roleId";

            try
            {
                SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@roleId", roleId));
                List<Mg_rrRelation> list = new List<Mg_rrRelation>();
                while (reader.Read())
                {
                    Mg_rrRelation mg_rrRelation = new Mg_rrRelation();

                    mg_rrRelation.Id = (int)reader["id"];
                    mg_rrRelation.RoleId = (string)reader["roleId"];
                    mg_rrRelation.RightsId = (int)reader["rightsId"];
                    list.Add(mg_rrRelation);


                }
                //reader.Close();
                return list;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
		
		
		
		
        private static IList<Mg_rrRelation> GetMg_rrRelationsBySql( string safeSql )
        {
            List<Mg_rrRelation> list = new List<Mg_rrRelation>();

			try
			{
				DataTable table = DBHelper.GetDataSet( safeSql );
				if(table.Rows.Count>0){
				foreach (DataRow row in table.Rows)
				{
					Mg_rrRelation mg_rrRelation = new Mg_rrRelation();
					
					mg_rrRelation.Id = Convert.ToInt32(row["id"]);
					mg_rrRelation.RoleId = row["roleId"].ToString();
					mg_rrRelation.RightsId = Convert.ToInt32(row["rightsId"]);
	
					list.Add(mg_rrRelation);
				}
	
				
			}
                return list;
            
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }

        }
		
        private static IList<Mg_rrRelation> GetMg_rrRelationsBySql( string sql, params SqlParameter[] values )
        {
            List<Mg_rrRelation> list = new List<Mg_rrRelation>();

			try
			{
				DataTable table = DBHelper.GetDataSet( sql, values );
				
				foreach (DataRow row in table.Rows)
				{
					Mg_rrRelation mg_rrRelation = new Mg_rrRelation();
					
					mg_rrRelation.Id = (int)row["id"];
					mg_rrRelation.RoleId = (string)row["roleId"];
					mg_rrRelation.RightsId = (int)row["rightsId"];
	
					list.Add(mg_rrRelation);
				}
	
				return list;
			}
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
			
        }

        public static IList<Mg_rrRelation> GetMg_rrRelationsBySqlDataset(string sql, params SqlParameter[] values)
        {
            return GetMg_rrRelationsBySql(sql,values);


        }

        public static IList<Mg_rrRelation> GetMg_rrRelationsBySqlDataset(string sql)
        {
            return GetMg_rrRelationsBySql(sql);


        }
		
	}

    
}
